package com.education.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import com.education.model.StudentModel;
import com.education.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.education.dao.CourseInformDao;
import com.education.model.CourseInform;
import com.education.service.ICourseServiceApi;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * ICourseServiceApi实现类
 * @author 赵睿慷
 *
 */
@Service
public class ICourseServiceApiImpl implements ICourseServiceApi{

    /**
     * 注入CourseInformDao接口
     */
    @Autowired
    private CourseInformDao courseInformDao ;

    /**
     * 2017/8/3 by 雷紫辉  增加参数，teacherId,只查询当前登陆的用户
     * @param courseName 传入课程名称
     * @param informId   传入课程通知编号
     * @param pageNo     当前页
     * @param pageSize   总数据条数
     * @return
     */
    @Override
    public PageInfo<CourseInform> selectInformById(int teacherId,String courseName,Integer informId,Integer pageNo, Integer pageSize) {
        // TODO Auto-generated method stub
        pageNo = pageNo == null?1:pageNo;
        pageSize = pageSize == null?15:pageSize;
        PageHelper.startPage(pageNo, pageSize);
        List<CourseInform> list = courseInformDao.selectInformById(teacherId,courseName,informId);
 
        PageInfo<CourseInform> page = new PageInfo<>(list);
        return page;
    }

    /**
     *修改 
     */
    @Override
    public int updateCourseById(CourseInform courseInform) {
        int update = courseInformDao.updateCourseById(courseInform);
        return update;
    }

    /**
     * 增加
     *
     */
    @Override
    public int addCourseById(CourseInform courseInform,String courseName) {

        /* 不知所云*/
        CourseInform courseInforms = new CourseInform();
        courseInforms.setCourseName(courseName);
        courseInformDao.addCourseByName(courseInforms);
        courseInform.setCourseId(courseInforms.getCourseId());
        courseInform.setCourseName(courseInforms.getCourseName());
        System.out.println("3333333:"+courseInforms.getCourseName());
        int addById = courseInformDao.addCourseById(courseInform);
        return addById;

    }

    /**
     * 2017/8/3 by 雷紫辉  新增方法替代 addCourseById
     * @param courseInform
     * @return
     */
    @Override
    public int addCourseInfo(CourseInform courseInform) {
        return courseInformDao.addCourseById(courseInform);
    }

    /**
     * 删除
     */
    @Override
    public int deleteCourseById(int informId) {
        // TODO Auto-generated method stub
        int del = courseInformDao.deleteCourseById(informId);
        return del;
    }


    /**
     * 通过日期返回查询
     * @param dataRange
     * @param teacherId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageInfo<CourseInform> searchInformByDataRange(String[] dataRange,Integer teacherId,Integer pageNum,Integer pageSize) throws ParseException {
        pageNum = pageNum == null?1:pageNum;
        pageSize = pageSize == null?15:pageSize;
        PageHelper.startPage(pageNum, pageSize);
        List<CourseInform> list;
        if (dataRange==null){
            list=courseInformDao.searchInformByDataRange(null,null,teacherId);
        }
        else {
            list=courseInformDao.searchInformByDataRange(DateUtil.UTC2GMT(dataRange[0]),DateUtil.UTC2GMT(dataRange[1]),teacherId);
        }

        PageInfo<CourseInform> pageInfo=new PageInfo<>(list);

        return pageInfo;
    }

    @Override
    public List<StudentModel> getStudentByTeacherId(int tId) {
        return courseInformDao.getStudentByTeacherId(tId);
    }

    /**
     * 增加(通过教师编号获取名称)
     */
    @Override
    public List<CourseInform> selectTeacherById(int teacherId) {
        // TODO Auto-generated method stub
        List<CourseInform> list = courseInformDao.selectTeacherById(teacherId);
        return list;
    }

    /**
     * 增加(通过课程编号获取名称)
     */
    @Override
    public String selectCourseById(int tId) {
        String select = courseInformDao.selectCourseById(tId);
        return select;
    }
    
}
